package ids;

import java.util.HashSet;
import java.util.Vector;

import ns2.Field;

import util.Connections;

public class ProbeSensor extends Sensor {

	public static int upper_threshold = 50;

	public static int lower_threshold = 5;

	public int nProbes;

	public Connections connections;

	public ProbeSensor() {
		super();
		this.nProbes = 0;
		this.cooperate = true;
	}

	public boolean alert() {
		return (this.nProbes > 0);
	}

	@Override
	public HashSet<Integer> getAttackersID() {
		return this.attackers;
	}

	@Override
	public HashSet<Integer> getVictimsID() {
		return this.victims;
	}

	@Override
	public float getScore() {
		int nAttackers = this.attackers.size();
		int nVictims = this.victims.size();
		int nTraffic = this.connections.size();
		return nAttackers * nVictims * 1000 + nTraffic;
	}

	@Override
	public void run() {
		this.reset();
		this.connections = this.location.connections;
		Vector<Connections> groupBySources = this.connections
				.sort(Field.Src_addr);
		for (int i = 0; i < groupBySources.size(); i++) {
			if (groupBySources.elementAt(i).size() >= lower_threshold) {
				this.nProbes++;
				this.attackers.add(groupBySources.elementAt(i).elementAt(0)
						.getSrc());
				for (int j = 0; j < groupBySources.elementAt(i).size(); j++) {
					this.victims.add(groupBySources.elementAt(i).elementAt(j)
							.getDst());
				}
			}
		}
	}

	@Override
	public void reset() {
		this.nProbes = 0;
		this.attackers.clear();
		this.victims.clear();
	}

	@Override
	public int getNumAttacks() {
		return this.nProbes;
	}

	@Override
	public SensorType getSensorType() {
		return SensorType.ProbeSensor;
	}

}
